﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.UI;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.Admin
{
	public partial class NewsList : AdminBasePage
	{
		/// <summary>
		/// 查看新闻的系统字典类型
		/// </summary>
		public string SystemType
		{
			get
			{
				//if (ViewState["SystemType"] == null)
				//    return "00020001";//要闻导读，默认值
				//else
				//    return ViewState["SystemType"].ToString();
				return this.SystemCode;
			}
			private set
			{
				ViewState["SystemType"] = this.SystemCode;
			}
		}

		/// <summary>
		/// 是否是扩展编辑
		/// </summary>
		public bool ExEdit
		{
			get
			{
				if(ViewState["EditType"] == null)
				{
					return false;
				}
				else
				{
					return Convert.ToBoolean(ViewState["EditType"]);
				}
			}
			private set
			{
				ViewState["EditType"] = value;
			}
		}

		/// <summary>
		/// 是否发布到英文站点
		/// </summary>
		public bool IsEN
		{
			get
			{
				if (ViewState["IsEN"] == null)
				{
					return false;
				}
				else
				{
					return Convert.ToBoolean(ViewState["IsEN"]);
				}
			}
			private set
			{
				ViewState["IsEN"] = value;
			}
		}

		/// <summary>
		/// 是否为专题活动
		/// </summary>
		public bool IsSpa
		{
			get
			{
				if (ViewState["IsSpa"] == null)
				{
					return false;
				}
				else
				{
					return Convert.ToBoolean(ViewState["IsSpa"]);
				}
			}
			private set
			{
				ViewState["IsSpa"] = value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			this.txt_KeyWords.Attributes.Add("onkeyup", "return DoLogin()");
			if (!Page.IsPostBack)
			{
				if (!string.IsNullOrWhiteSpace(Request["st"]))
				{
					this.SystemType = Request["st"].Trim();
				}

				if (!string.IsNullOrWhiteSpace(Request["ex"]))
				{
					this.ExEdit = Request["ex"].Trim() == "1";
				}
				else
				{
					this.ExEdit = false;
				}

				if (!string.IsNullOrWhiteSpace(Request["en"]))
				{
					this.IsEN = Request["en"].Trim() == "1";
				}
				else
				{
					this.IsEN = false;
				}

				if (!string.IsNullOrWhiteSpace(Request["x"]))
				{
					this.IsSpa = Request["x"].Trim() == "1";
				}
				else
				{
					this.IsSpa = false;
				}
				BindData(1, "");
			}
		}

		private void BindData(int pageIndex,string keywords)
		{
			Expression<Func<TB_News, bool>> f;
			if (string.IsNullOrWhiteSpace(keywords))
			{
				f = (p => p.Type.ToLower() == this.SystemType.ToLower() && p.IsEn == this.IsEN);
			}
			else
			{
				f = (p => p.Type.ToLower() == this.SystemType.ToLower() && p.IsEn == this.IsEN && p.Title.Contains(keywords));
			}
			
			Expression<Func<TB_News, DateTime>> o = (p => p.CreateDate);
			Expression<Func<TB_News, int>> then = (p => p.ID);
			int records = 0;
			var newsList = DAL.DALHelper.Instance.GetList<TB_News, DateTime, int>(f, o, false, then, false, this.Pager1.PageSize, pageIndex, out records);
			this.rpt_News.DataSource = newsList;
			this.rpt_News.DataBind();
			this.Pager1.RecordCount = records;
			
			this.Pager1.InitControl();
		}

		public void BindSelectedPageIndexChanged(int pageIndex, int nextPage)
		{
			string keywords = this.Hid_Keywords.Value.Trim();
			this.txt_KeyWords.Value = keywords;
			BindData(nextPage, keywords);
		}

		protected void btn_Delete_Click(object sender, EventArgs e)
		{
			int newsID = 0;
			if(!string.IsNullOrWhiteSpace(this.hid_ID.Value) && int.TryParse(this.hid_ID.Value,out newsID))
			{
				string keywords = this.Hid_Keywords.Value.Trim() ;
				this.txt_KeyWords.Value = keywords;
				DAL.DALHelper.Instance.DeleteWithTransaction<TB_News>("ID", newsID);
				
				int CurrentPage = 1;// 默认第一页开始。
				if (this.Pager1.CurrentPageIndex > 1)
				{
					int NumberCount = this.Pager1.RecordCount - (this.Pager1.CurrentPageIndex - 1) * this.Pager1.PageSize;
					if (NumberCount > 0)
					{
						if (NumberCount > 1)// 判断当前页的记录是否只剩一条。
						{
							CurrentPage = this.Pager1.CurrentPageIndex;
						}
						else
						{
							CurrentPage = this.Pager1.CurrentPageIndex - 1;
						}
					}
				}

				this.Pager1.CurrentPageIndex = CurrentPage;
				BindData(CurrentPage, keywords);
			}
			this.hid_ID.Value = string.Empty;
		}

		protected void btn_NewsRefresh_Click(object sender, EventArgs e)
		{
			string keywords = this.Hid_Keywords.Value.Trim();
			this.txt_KeyWords.Value = keywords;
			BindData(this.Pager1.CurrentPageIndex,keywords);
		}

		protected void btn_Search_Click(object sender, EventArgs e)
		{
			string keywords = this.txt_KeyWords.Value.Trim();
			this.Pager1.CurrentPageIndex = 1;
			BindData(1, keywords);
		}
	}
}